const async = require('async');

module.exports = {
    open:false,
    description:'获取点赞',
    handler(req, res, next) {
        let doc = req.query;
        let userID = doc.filter.userID;
        let page = doc.page;
        let skip = 0;
        let limit = 10;
        if (page) {
            if (page.size) {
                limit = page.size;
                if (page.index > 1) {
                    skip = (page.index - 1) * page.size;
                }
            }
        }
        async.auto([
                callback => req.mongo.collection("card").aggregate([
                    {$unwind: "$likeID"},
                    {$match: {"likeID": userID, "checkState": 1}}
                ]).toArray(callback)
            ],
            (err, results) => {
                if (err) return res.err.mongoError(err);
                var card = results[0] || [];
                if (!page) {
                    return res.reply(card);
                }
                let count = card.length;
                page.size = page.size || 10;
                page.index = page.index || 1;
                page.count = count;
                page.pageCount = Math.ceil(page.count / page.size);
                if (count <= 0) return res.reply(page);
                page.hasPrevious = page.index > 1;
                page.hasNext = page.index < page.pageCount;
                page.index = page.pageCount < page.index ? page.pageCount : page.index;
                page.items = card.slice(skip, skip + limit);
                return res.reply({code: 0, message: "查询成功", data: page});
            }
        )
    }
};
